import { openWindow } from "@/utils/index";
import { dataURLtoBlob } from "./base64Conver";

/**
 * 基于base64下载图片
 * @param buf
 * @param filename
 * @param mime
 * @param bom
 */
export function downloadByBase64(
  buf: string,
  filename: string,
  mime?: string,
  bom?: BlobPart
) {
  const base64Buf = dataURLtoBlob(buf);
  downloadByData(base64Buf, filename, mime, bom);
}

/**
 * 根据后台接口文件流下载
 * @param {*} data
 * @param {*} filename
 * @param {*} mime
 * @param {*} bom
 */
export function downloadByData(
  data: BlobPart,
  filename: string,
  mime?: string,
  bom?: BlobPart
) {
  const blobData = typeof bom !== "undefined" ? [bom, data] : [data];
  const blob = new Blob(blobData, { type: mime || "application/octet-stream" });

  const blobURL = window.URL.createObjectURL(blob);
  const tempLink = document.createElement("a");
  tempLink.style.display = "none";
  tempLink.href = blobURL;
  tempLink.setAttribute("download", filename);
  if (typeof tempLink.download === "undefined") {
    tempLink.setAttribute("target", "_blank");
  }
  document.body.appendChild(tempLink);
  tempLink.click();
  document.body.removeChild(tempLink);
  window.URL.revokeObjectURL(blobURL);
}

/**
 * 根据文件地址下载文件
 * @param {*} sUrl
 */
declare type TargetContext = "_self" | "_blank";
export function downloadByUrl({
  url,
  target = "_blank",
  fileName,
}: {
  url: string;
  target?: TargetContext;
  fileName?: string;
}): boolean {
  const isChrome =
    window.navigator.userAgent.toLowerCase().indexOf("chrome") > -1;
  const isSafari =
    window.navigator.userAgent.toLowerCase().indexOf("safari") > -1;

  if (/(iP)/g.test(window.navigator.userAgent)) {
    console.error("Your browser does not support download!");
    return false;
  }
  if (isChrome || isSafari) {
    const link = document.createElement("a");
    link.href = url;
    link.target = target;

    if (link.download !== undefined) {
      link.download =
        fileName || url.substring(url.lastIndexOf("/") + 1, url.length);
    }

    if (document.createEvent) {
      const e = document.createEvent("MouseEvents");
      e.initEvent("click", true, true);
      link.dispatchEvent(e);
      return true;
    }
  }
  if (url.indexOf("?") === -1) {
    url += "?download";
  }

  openWindow(url, { target });
  return true;
}

/**
 * @desc 基于JSON数据导出JSON文件
 */

export function downloadExportJoson(
  jsonData: any,
  fileName: string,
  version: string | number
) {
  const data = JSON.stringify(jsonData);
  // encodeURIComponent 解决中文乱码
  const uri = `data:text/csv;charset=utf-8,\ufeff${encodeURIComponent(data)}`;
  //通过创建a标签实现
  const link = document.createElement("a");
  link.href = uri;
  //对下载的文件命名
  const jsonName = `${fileName}-version-${version}.json`;
  link.download = jsonName;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

/**
 *
 * @param fileUrl 下载路径
 * @param fileName 文件名称
 * @param suffix 文件后缀
 */
export function downloadFile(
  fileUrl: string,
  fileName: string,
  suffix: string
) {
  const a = document.createElement("a");
  a.download = `${fileName}.${suffix ? suffix : ".xlsx"}`;
  a.href = fileUrl;
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  a.remove();
}
